<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 JVM 类加载器机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 14px;
            color: #888;
            border-bottom: 1px solid #3e3e3e;
        }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        
        .section-divider {
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
            margin: 2rem auto;
            border-radius: 2px;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea, #764ba2);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        .highlight-text {
            background: linear-gradient(120deg, #f093fb 0%, #f5576c 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    深入理解 JVM 类加载器机制
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    探索 Java 虚拟机中类加载器的核心原理与自定义实现
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-clock mr-2"></i>阅读时间：8分钟
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-layer-group mr-2"></i>深度技术文章
                    </span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>JVM 中，类加载器（ClassLoader）负责动态加载类文件（.class 文件）到内存中，并将它们转换为 Java 类对象。类加载器在 JVM 的运行时环境中扮演着至关重要的角色，是 Java 平台独立性和动态性的基石。
            </p>
        </div>

        <!-- 类加载器的作用 -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <i class="fas fa-cogs mr-4 text-purple-600"></i>
                类加载器的作用
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-download"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">动态加载类</h3>
                    <p class="text-gray-600 leading-relaxed">
                        类加载器从磁盘或其他源（如网络、数据库）中读取类文件，并将其加载到 JVM 的内存中。这样可以在程序运行时动态地加载和使用类，而不必在编译时就将所有类固定下来。
                    </p>
                </div>

                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-link"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">类的链接</h3>
                    <div class="space-y-2">
                        <p class="text-gray-600"><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>验证：</strong>确保加载的类文件符合 JVM 的安全性和规范要求</p>
                        <p class="text-gray-600"><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>准备：</strong>为类变量分配内存并设置默认初始值</p>
                        <p class="text-gray-600"><i class="fas fa-check-circle text-green-500 mr-2"></i><strong>解析：</strong>将类中的符号引用转换为直接引用</p>
                    </div>
                </div>

                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-play-circle"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">类的初始化</h3>
                    <p class="text-gray-600 leading-relaxed">
                        执行类的静态初始化块和静态变量初始化。这是类的生命周期中的一个重要阶段，确保类在使用前已经完成必要的初始化工作。
                    </p>
                </div>

                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-shield-alt"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">隔离和委派</h3>
                    <p class="text-gray-600 leading-relaxed">
                        类加载器提供了类加载的隔离机制，确保不同的类加载器加载的类之间不会冲突。类加载器遵循"委派模型"，即类加载请求首先被委托给父类加载器处理。
                    </p>
                </div>
            </div>
        </section>

        <!-- 类加载器层次结构图 -->
        <section class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-2xl p-8 mb-12">
            <h3 class="text-2xl font-bold mb-6 text-center text-gray-800">类加载器层次结构</h3>
            <div class="mermaid">
                graph TD
                    A[Bootstrap ClassLoader<br/>启动类加载器] --> B[Extension ClassLoader<br/>扩展类加载器]
                    B --> C[Application ClassLoader<br/>应用类加载器]
                    C --> D[Custom ClassLoader<br/>自定义类加载器]
                    
                    style A fill:#667eea,stroke:#5a67d8,stroke-width:2px,color:#fff
                    style B fill:#764ba2,stroke:#6b46c1,stroke-width:2px,color:#fff
                    style C fill:#f093fb,stroke:#e879f9,stroke-width:2px,color:#fff
                    style D fill:#f5576c,stroke:#ef4444,stroke-width:2px,color:#fff
            </div>
        </section>

        <!-- 自定义类加载器的实现 -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <i class="fas fa-code mr-4 text-purple-600"></i>
                自定义类加载器的实现
            </h2>
            
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
                <p class="text-lg leading-relaxed text-gray-700 mb-6">
                    自定义类加载器可以用于实现特定的类加载策略，例如动态插件系统、模块化系统或者在不同的类加载上下文中隔离类。要实现自定义类加载器，你需要继承 <code class="bg-gray-100 px-2 py-1 rounded text-purple-600">java.lang.ClassLoader</code> 类，并重写其关键方法。
                </p>
            </div>

            <!-- 步骤1：创建自定义类加载器 -->
            <div class="mb-8">
                <h3 class="text-2xl font-semibold mb-4 text-gray-800">
                    <span class="highlight-text">步骤 1：</span> 创建自定义类加载器
                </h3>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fab fa-java mr-2"></i>MyClassLoader.java
                    </div>
                    <pre><code class="language-java">public class MyClassLoader extends ClassLoader {
    private String classPath;

    public MyClassLoader(String classPath) {
        this.classPath = classPath;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        // 1. 将类名转换为类文件的路径
        String fileName = name.replace('.', '/') + ".class";
        byte[] classData = loadClassData(fileName);

        if (classData == null) {
            throw new ClassNotFoundException(name);
        } else {
            return defineClass(name, classData, 0, classData.length);
        }
    }

    private byte[] loadClassData(String fileName) {
        // 实现类文件的读取逻辑（例如从文件系统中读取）
        try (InputStream inputStream = new FileInputStream(new File(classPath, fileName))) {
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byteStream.write(buffer, 0, bytesRead);
            }
            return byteStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}</code></pre>
                </div>
            </div>

            <!-- 步骤2：使用自定义类加载器 -->
            <div class="mb-8">
                <h3 class="text-2xl font-semibold mb-4 text-gray-800">
                    <span class="highlight-text">步骤 2：</span> 使用自定义类加载器
                </h3>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fab fa-java mr-2"></i>Main.java
                    </div>
                    <pre><code class="language-java">public class Main {
    public static void main(String[] args) throws Exception {
        MyClassLoader myClassLoader = new MyClassLoader("path/to/classes");

        // 通过自定义类加载器加载类
        Class<?> clazz = myClassLoader.loadClass("com.demo.MyClass");
        Object instance = clazz.getDeclaredConstructor().newInstance();
        
        // 调用类的方法
        Method method = clazz.getMethod("myMethod");
        method.invoke(instance);
    }
}</code></pre>
                </div>
            </div>
        </section>

        <!-- 自定义类加载器的关键点 -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center">
                <i class="fas fa-key mr-4 text-purple-600"></i>
                自定义类加载器的关键点
            </h2>

            <div class="grid gap-6">
                <div class="bg-gradient-to-r from-purple-500 to-pink-500 p-1 rounded-xl">
                    <div class